package cn.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.shop.admin.service.OrderRewardService;
import cn.shop.transcation.order.entity.OrderReward;
import cn.shop.transcation.order.entity.OrderRewardQueryReq;
import cn.shop.utils.ApiResult;
import cn.shop.utils.Putif;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 奖励查询接口
 */
@RestController
@RequestMapping("/api/v1/reward")
public class OrderRewardController {

    @Resource
    private OrderRewardService rewardService;

    /**
     * 通过条件查询奖励
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(@Validated OrderRewardQueryReq req) {
        return ApiResult.success(rewardService.listPage(req));
    }

    /**
     * 通过条件统计奖励
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/statistics")
    public ApiResult<Map<String, Object>> statistics(OrderRewardQueryReq req) {
        return ApiResult.success(rewardService.statistics(req));
    }

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     * @author jzw
     * @since 2021-12-06 14:22:11
     */
    @GetMapping("/queryRWReward")
    public ApiResult<List<Map<String, Object>>> queryReward() {
        List<OrderReward> list = rewardService.queryReward(Arrays.asList(0, 1));
        return ApiResult.success(list.stream().map(r -> Putif.start()
                .anyway("amount", r.getAmount())
                .anyway("type", r.getType())
                .anyway("account", StrUtil.hide(r.getWebAccount(), 1, r.getWebAccount().length() + 1))
                .get()).collect(Collectors.toList()));
    }

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     * @author jzw
     * @since 2021-12-06 14:22:11
     */
    @GetMapping("/queryOrderReward")
    public ApiResult<List<Map<String, Object>>> queryOrderReward() {
        List<OrderReward> list = rewardService.queryReward(Collections.singletonList(2));
        return ApiResult.success(list.stream().map(r -> Putif.start()
                .anyway("amount", r.getAmount())
                .anyway("type", r.getType())
                .anyway("account", StrUtil.hide(r.getWebAccount(), 1, r.getWebAccount().length() + 1))
                .get()).collect(Collectors.toList()));
    }

}
